package com.ys.controller.web.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.entity.sys.SysRole;
import com.ys.service.sys.SysRoleService;
import com.ys.vo.W;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/web/sys/sysRole")
public class WebSysRoleController {
	
	@Autowired
	private SysRoleService sysRoleService;
	
	/**
	 * 新增子角色
	 */
	@GetMapping("/toAddSubSysRole")
	public String toAddSubSysRole(Model model, Long pid){
		if (pid == null) {
			return W.error(model, "上级角色不能为空");
		}
		
		SysRole sysRole = new SysRole().setId(pid).queryFirst();
		String pname = sysRole.getName();
		ToAddSubSysRoleDto dto = new ToAddSubSysRoleDto(pid, pname);
		
		model.addAttribute("dto", dto);
		return "sys/sysRole/toAddSubSysRole";
	}
	@Data
	@AllArgsConstructor
	private static class ToAddSubSysRoleDto{
		private Long pid; // 父角色ID
		private String pname; // 父角色名称
	}

	/*
	 * 编辑角色（基础信息）
	 */
	@GetMapping("/toEditSysRole")
	public String toEditSysRole(Model model, HttpServletRequest request, HttpServletResponse response, 
			Long id){
		return sysRoleService.toEditSysRole(model, id);
	}

	/*
	 * 角色授权
	 */
	@GetMapping("/toAuthSysRole")
	public String toAuthSysRole(Model model, HttpServletRequest request, HttpServletResponse response,
			Long sysRoleId){
		
		AuthDto dto = new AuthDto(sysRoleId);
		model.addAttribute("dto", dto);
		return "sys/sysRole/toAuthSysRole";
	}
	@Data
	@AllArgsConstructor
	public static class AuthDto {
		private Long sysRoleId; // 角色ID
	}
}
